﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Ionic.Zip;
using System.Diagnostics;

namespace Updater
{
    class applicationContext : ApplicationContext
    {
        NotifyIcon ni = null;
        string applicationName;
        string filename;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="args[0]">folder</param>
        /// <param name="args[1]">filename</param>
        public applicationContext(string[] args)
        {
            filename = args[1];
            applicationName = args[0];
            if (!FtpUploader.DownloadFile(args[0], args[1]))
                Application.Exit();

            ni = new NotifyIcon();
            ni.Icon = Properties.Resources.App;
            ni.BalloonTipIcon = ToolTipIcon.Info;
            ni.BalloonTipTitle = string.Format(Properties.Resources.Msg_NewVersion, applicationName);
            ni.BalloonTipText = Properties.Resources.Msg_ClickBaloon;
            ni.Visible = true;
            ni.ShowBalloonTip(30000);
            ni.MouseClick += new MouseEventHandler(ni_MouseClick);
            ni.BalloonTipClicked += new EventHandler(ni_BalloonTipClicked);
        }

        void ni_BalloonTipClicked(object sender, EventArgs e)
        {
            if (ProcessRun())
            {
                MessageBox.Show(Properties.Resources.Msg_Waiting);
            }
            else
            {
                Update();
                Exit();
            }
        }

        void ni_MouseClick(object sender, MouseEventArgs e)
        {
            ni.ShowBalloonTip(30000);
        }

        void Update()
        {
            using (ZipFile zipfile = new ZipFile(filename))
            {
                zipfile.ExtractAll(".", ExtractExistingFileAction.OverwriteSilently);
            }
            File.Delete(filename);
        }

        private bool ProcessRun()
        {
            DialogResult result = DialogResult.Cancel;
            bool processExists = false;
            string message = string.Format(Properties.Resources.Msg_CloseProgram, applicationName);
            do
            {
                processExists = Process.GetProcesses().Any(p => p.ProcessName.Equals("ApplicationManager"));
                if (processExists)
                    result = MessageBox.Show(message, Properties.Resources.Msg_Warning, MessageBoxButtons.RetryCancel, MessageBoxIcon.Warning);
            }
            while (processExists && result == DialogResult.Retry);

            return processExists;
        }

        private void Exit()
        {
            ni.Visible = false;
            ni.Dispose();
            MessageBox.Show(string.Format(Properties.Resources.Msg_Updated, applicationName), applicationName, MessageBoxButtons.OK, MessageBoxIcon.Information);
            Application.Exit();
        }
    }
}